﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.HtmlHelpers
{
    public static class MenuHelper
    {  
        public static IHtmlString Menu(this HtmlHelper helper, IEnumerable<MenuItem> menuItems)
        {
            var writer = new HtmlTextWriter(new StringWriter());
            var currentUrl = HttpContext.Current.Request.RawUrl;
            var currentUrlSubStr = currentUrl.Split('/');
            var controllerName = currentUrlSubStr.ElementAtOrDefault(1);
            var actionName = currentUrlSubStr.ElementAtOrDefault(2);
            var defaultRoute = GetDefaultRouteObject(helper);
            currentUrl = "/" + (string.IsNullOrEmpty(controllerName) ? defaultRoute["controller"] : controllerName) + 
                "/" + (string.IsNullOrEmpty(actionName) ? defaultRoute["action"] : actionName);

            menuItems = menuItems.OrderBy(o => o.Sequence);


            // Open UL
            writer.AddAttribute("class", "nav nav-list");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            writer.WriteLine();
            foreach (var item in menuItems)
            {
                switch (item.MenuLevel)
                {
                    case MenuLevel.Head:

                        if (currentUrl.ToLower().Equals(item.Url.ToLower()) ||
                            (
                            item.SubMenu != null && item.SubMenu.Any(x => currentUrl.ToLower().Equals(x.Url.ToLower()))))
                        {
                            writer.AddAttribute("class", "active open");
                        }
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                        writer.AddAttribute("href", "#");
                        writer.AddAttribute("class", "dropdown-toggle");
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.AddAttribute("class", item.Icon ?? "icon-list-alt");
                        writer.RenderBeginTag(HtmlTextWriterTag.I);
                        writer.RenderEndTag();
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);
                        writer.Write(helper.Encode(item.Name));
                        writer.RenderEndTag();
                        writer.AddAttribute("class", "arrow icon-angle-down");
                        writer.RenderBeginTag(HtmlTextWriterTag.B);
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        writer.WriteLine();

                        writer.AddAttribute("class", "submenu");
                        writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                        if(item.SubMenu != null)
                        foreach (var subItem in item.SubMenu)
                        {
                            if (currentUrl.ToLower().Equals(subItem.Url.ToLower())) writer.AddAttribute("class", "active");
                            writer.RenderBeginTag(HtmlTextWriterTag.Li);
                            writer.AddAttribute("href", (subItem).Url);
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.AddAttribute("class", "icon-double-angle-right");
                            writer.RenderBeginTag(HtmlTextWriterTag.I);
                            writer.RenderEndTag();
                            writer.Write(helper.Encode(subItem.Name));
                            writer.RenderEndTag();
                            writer.RenderEndTag();
                            writer.WriteLine();
                        }
                        writer.RenderEndTag();
                        writer.WriteLine();
                        writer.RenderEndTag();
                        writer.WriteLine();
                        break;
                    case MenuLevel.Main:
                        if (currentUrl.ToLower().Equals(item.Url.ToLower())) writer.AddAttribute("class", "active");
                        writer.RenderBeginTag(HtmlTextWriterTag.Li);
                        writer.AddAttribute("href", (item).Url);
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.AddAttribute("class", item.Icon ?? "icon-list-alt");
                        writer.RenderBeginTag(HtmlTextWriterTag.I);
                        writer.RenderEndTag();
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);
                        writer.Write(helper.Encode(item.Name));
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                        writer.WriteLine();
                        break;
                }
            }
            // Close UL
            writer.RenderEndTag();
            writer.WriteLine();

            // Return the HTML string
            return helper.Raw(writer.InnerWriter.ToString());
        }


        public static IEnumerable<MenuItem> MenuItemLists
        {
            get
            {
                var l = new List<MenuItem>();

                var i1 = new MenuItem(UrlType.MVCUrl, MenuLevel.Head) { MenuItemId = 1, Name = "ชื่อ 1" };
                var i2 = new MenuItem(UrlType.MVCUrl) { MenuItemId = 2, Name = "ชื่อ 2", MvcController = "Home2", MvcAction = "Index" };
                var i3 = new MenuItem(UrlType.RootUrl) { MenuItemId = 3, Name = "ชื่อ 3", MvcController = "Home2", MvcAction = "Index" };
                var i4 = new MenuItem(UrlType.MVCUrl) { MenuItemId = 4, Name = "ชื่อ 4", MvcController = "Home2", MvcAction = "Index" };
                var i5 = new MenuItem(UrlType.MVCUrl) { MenuItemId = 5, Name = "RecordsPage", MvcController = "Mai", MvcAction = "Index" };

                l.Add(i1);
                l.Add(i2);
                l.Add(i3);
                l.Add(i4);
                l.Add(i5);

                return l;

            }
        }

        public static RouteValueDictionary GetDefaultRouteObject(HtmlHelper helper)
        {
            return (from Route route in helper.RouteCollection let routeData = route.GetRouteData(helper.ViewContext.HttpContext) where routeData != null select route.Defaults).FirstOrDefault();
        }
    }
}